package com.baihejia.common.dubbo.server.libs;

import com.baihejia.common.dubbo.server.Configuration;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.util.Arrays;

public class Constants {

	public static final String EMPTY_CACHE_VALUE_FLAG = "###+--**-+###";

	public static final String CACHE_KEY_PREFIX = "bhj_thrift";

	public static final long _1Second = 1000;

	public static final int _PageSize = 60;
	
	public static final int _RecommendNum = 4;//二手房好房推荐
	
	public static final long _1Minute = 60 * _1Second;

	public static final long _1Hour = 60 * _1Minute;

	public static final long _1Day = 24 * _1Hour;

	public final static ObjectMapper sharedMapper = new ObjectMapper();

	public static final DecimalFormat df = new DecimalFormat("###################.##");

	public static final DecimalFormat dfNum = new DecimalFormat("100000000");
	
	static {
		sharedMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		sharedMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		sharedMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
	}

	private static final double EARTH_RADIUS = 6378137;// 赤道半径(单位m)

	public static final String PREFIX = Configuration.get("application.name");

	public static String getCacheKey(Class<?> cls, Object id) {
		return String.format("%s_%s_%s", PREFIX, cls.getSimpleName(), id);
	}

	/**
	 * 基于googleMap中的算法得到两经纬度之间的距离,计算精度与谷歌地图的距离精度差不多，相差范围在0.2米以下
	 *
	 * @param lng1
	 *            第一点的经度
	 * @param lat1
	 *            第一点的纬度
	 * @param lng2
	 *            第二点的经度
	 * @param lat2
	 *            第二点的纬度
	 * @return 返回的距离，单位米
	 */
	public static double getDistance(double lng1, double lat1, double lng2, double lat2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(
				Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

	/**
	 * 转化为弧度(rad)
	 */
	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	/**
	 * double四舍五入保留两位
	 */
	public static String doubleFormat(Double d) {
		return df.format(d);
	}

	/**
	 * double转%
	 */
	public static String doublePercent(Double d) {
		return df.format(d * 100) + "%";
	}
	
	/**
	 * id转编号
	 * @return
	 */
	public static String getNumber(long id){
		return dfNum.format(id);
	}

	public static boolean forbiddenCommunity(Long communityId) {
		String communities = Configuration.get("forbiddenCommunity");
		return !StringUtils.isBlank(communities) && Arrays.asList(communities.split(",")).stream().anyMatch(forbiddenCommunity -> StringUtils.trim(forbiddenCommunity).equals(String.valueOf(communityId)));
	}
}
